package com.iss.examsys.view.util;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

@FacesConverter(value = "QuestionConverter")
public class QuestionConverter implements Converter {

    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if (value != null && value.trim().length() > 0) {
            PickList p = (PickList) uic;
            DualListModel dl = (DualListModel) p.getValue();
            return dl.getSource().get(Integer.valueOf(value));
        }
        return null;
    }

    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if (object == null || object.equals("")) {
            return "";
        } else {
            PickList  p = (PickList) uic;
            DualListModel dl = (DualListModel) p.getValue();
            return  String.valueOf(dl.getSource().indexOf(object));
        }
    }
}
